package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.dao.DaoUtil;
import com.xishu.entity.Company;
import com.xishu.entity.config.BackConfig;
import com.xishu.entity.shop.Shop;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.*;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@Api(description = "备份配置")
@RestController
public class BackConfigController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(BackConfigController.class);

    @ApiOperation(value = "创建备份配置")
    @PostMapping(value = ("/user/backConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true, checkShopId = true)
    public ResponseData create(@RequestBody BackConfig backConfig) throws Exception {
        VerifyUtil.verify(() -> isNotEmpty(backConfig.getShopId()));
        Company company = CompanyService.getInstance().findCompanyByShopId(backConfig.getShopId());
        backConfig.setCompanyName(company.getName());
        return CommonService.getInstance().createObject(backConfig, (BackConfig b) -> {
            try {
                BackConfigService.getInstance().back(b);
            } catch (Exception e) {
                logger.error("e", e);
            }
        }, false, null);
    }

    @ApiOperation(value = "备份恢复")
    @PostMapping(value = ("/user/backConfig/restore"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData restore(@RequestBody BackConfig backConfig) throws Exception {
        BackConfigService backConfigService = BackConfigService.getInstance();
        BackConfig backConfigInDb = DaoUtil.getInstance().fetchById(backConfig);
        //同一个店，同步恢复
        Long shopId = backConfigInDb.getShopId();
        String backupLockKey = "back.up.lock.key." + shopId;
        Object lock = LockService.getInstance().getLock(backupLockKey);
        
        synchronized (lock) {
            backConfigService.restoreShop(backConfigInDb);
            return ResponseData.emptyResponse();
        }
    }

    @ApiOperation(value = "删除备份")
    @DeleteMapping(value = ("/user/backConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(checkShop = true)
    public ResponseData delete(@RequestBody BackConfig backConfig) throws Exception {
        return CommonService.getInstance().delete(backConfig);
    }

    @ApiOperation(value = "查询备份配置")
    @PutMapping(value = ("/user/backConfig"), produces = ("application/json;charset=UTF-8"))
    public ResponseData search(@RequestBody BackConfig backConfig) throws Exception {
        return CommonService.getInstance().searchResponse(backConfig, (b) -> {
            try {
                Long shopId = b.getShopId();
                Shop shop = ShopService.getInstance().findShopById(shopId);
                b.setShopName_zh(shop.getName_zh());
                b.setShopName_en(shop.getName_en());
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

}
